package com.java.auth.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java.common.utils.R;
import com.java.auth.service.SysRoleService;

import com.java.model.system.SysRole;
import com.java.vo.system.AssginRoleVo;
import com.java.vo.system.SysRoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation("查询所有角色信息")
    @GetMapping("/findAll")
    public R findAll(){
        List<SysRole> list = sysRoleService.list();
        return R.ok(list);
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("/{page}/{limit}")
    public R pageQueryRole(@PathVariable("page")Long page, @PathVariable("limit")Long limit, SysRoleQueryVo sysRoleQueryVo){
        IPage<SysRole> iPage = new Page<>(page,limit);
        R r = sysRoleService.pageSelectByLike(iPage, sysRoleQueryVo);
        return r;
    }
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation("添加角色")
    @PostMapping("/save")
    public R save(@RequestBody @Validated SysRole sysRole, BindingResult result){
        if (result.hasErrors()) {
            R<Object> r = R.fail().message("参数错误，无法添加！");
            return r;
        }
        sysRole.setDescription(sysRole.getRoleName());
        boolean save = sysRoleService.save(sysRole);
        if(save){
            return R.ok().message("保存成功！");
        }
        return R.fail().message("保存失败！");
    }
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("根据id查询角色")
    @GetMapping("/get/{id}")
    public R get(@PathVariable Long id){

        SysRole sysRole = sysRoleService.getById(id);
        return R.ok(sysRole);
    }
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation("修改角色")
    @PostMapping("/update")
    public R update(@RequestBody @Validated SysRole sysRole, BindingResult result){
        if (result.hasErrors()) {
            R<Object> r = R.fail().message("参数错误，无法修改！");
            return r;
        }
        boolean update = sysRoleService.updateById(sysRole);
        if(update){
            return R.ok().message("修改成功！");
        }
        return R.fail().message("修改失败！");
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("根据id进行角色删除")
    @DeleteMapping("/remove/{id}")
    public R remove(@PathVariable Long id){
        boolean is_success = sysRoleService.removeById(id);
        if(is_success){
            return R.ok().message("删除成功！");
        }
        return R.fail().message("删除失败！");
    }
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("批量删除")
    @DeleteMapping("/batchRemove")
    public R batchRemove(@RequestBody List<Long> ids){
        boolean is_success = sysRoleService.removeByIds(ids);
        if(is_success){
            return R.ok().message("删除成功！");
        }
        return R.fail().message("删除失败！");
    }
    @GetMapping("/toAssign/{userId}")
    @ApiOperation("根据用户获取角色数据")
    public R toAssign(@PathVariable Long userId){
         return sysRoleService.findRoleById(userId);
    }

    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public R doAssign(@RequestBody AssginRoleVo assginRoleVo) {
       return sysRoleService.doAssign(assginRoleVo);
    }


}
